﻿/*
 PennyAuctionTracker
 Copyright (C) 2011  Devin Bileck
 
 This program is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free Software Foundation;
 either version 2 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with this program;
 if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

using System;
using System.Collections;
using System.Net;

namespace PennyAuctionTracker
{
	/// <summary>
	/// Implements a unique penny auction.
	/// </summary>
	public abstract class Auction
	{
		/// <summary>
		/// The auction identifier.
		/// </summary>
		protected String auctionID;
		
		/// <summary>
		/// Stores all bids for the auction.
		/// </summary>
		public ArrayList BidHistory { get { return bidHistory; } }
		protected ArrayList bidHistory = new ArrayList();
		
		/// <summary>
		/// Stores each bidder that has participated in the auction.
		/// </summary>
		public ArrayList Bidders { get { return bidders; } }
		protected ArrayList bidders = new ArrayList();
		
		/// <summary>
		/// Represents an individual bid.
		/// </summary>
		public struct Bid
		{
			/// <summary>
			/// Name of the user that placed the bid.
			/// </summary>
			public String Bidder;
			
			/// <summary>
			/// Price of the bid.
			/// </summary>
			public String Price;
			
			/// <summary>
			/// Type of bid (i.e. manual or automatic).
			/// </summary>
			public String Type
			{
				get
				{
					if (type.Equals(Convert.ToString(MANUAL_BID))) return "Manual";
					else if (type.Equals(Convert.ToString(AUTOMATIC_BID))) return "Automatic";
					else return type;
				}
				set { type = value; }
			}
			private String type;
		}
		
		/// <summary>
		/// Enumerated value representing a manual bid.
		/// </summary>
		protected const int MANUAL_BID = 1;
		
		/// <summary>
		/// Enumerated value representing an automatic bid.
		/// </summary>
		protected const int AUTOMATIC_BID = 2;
		
		/// <summary>
		/// Represents a bidder.
		/// </summary>
		public struct Bidder
		{
			/// <summary>
			/// The name of the bidder.
			/// </summary>
			public String Name;
			
			/// <summary>
			/// The number of bids the user has placed on the auction.
			/// </summary>
			public int Bids;
		}
		
		/// <summary>
		/// Time remaining in the auction.
		/// </summary>
		public double TimeRemaining
		{
		   get { return timeRemaining; }
		}
		protected double timeRemaining = 0;
		
		/// <summary>
		/// Specifies if the auction parameters are valid.
		/// </summary>
		public bool isValid = false;
		
		/// <summary>
		/// Class constructor.
		/// </summary>
		public Auction()
		{ }
		
		/// <summary>
		/// Updates the auction.
		/// </summary>
		public abstract void Update();
		
		/// <summary>
		/// Returns a response from an internet resource.
		/// </summary>
		/// <param name="URI">The URI that identifies the internet resource.</param>
		/// <returns></returns>
		protected HttpWebResponse GetHttpWebResponse(String URI)
		{
			HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URI);
			httpWebRequest.Method = WebRequestMethods.Http.Get;
			httpWebRequest.AllowAutoRedirect = false;
			try
			{
				HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
				return response;
			}
			catch
			{
				return null;
			}
		}
		
		/// <summary>
		/// Returns a response from an internet resource.
		/// </summary>
		/// <param name="URI">The URI that identifies the internet resource.</param>
		/// <param name="Headers">An array of headers to include in the request ("name:value").</param>
		/// <returns></returns>
		protected HttpWebResponse GetHttpWebResponse(String URI, String[] Headers)
		{
			HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URI);
			httpWebRequest.Method = WebRequestMethods.Http.Get;
			httpWebRequest.AllowAutoRedirect = false;
			foreach (String header in Headers)
			{
				String[] param = header.Split(':');
				httpWebRequest.Headers[param[0]] = param[1];
			}
			try
			{
				HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
				return response;
			}
			catch
			{
				return null;
			}
		}
	}
}